package com.gxa.controller;


import com.github.pagehelper.PageInfo;
import com.gxa.pojo.Hospital;
import com.gxa.service.HospitalService;
import com.gxa.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/hospital")
public class HospitalController {

    @Resource
    HospitalService hospitalService=null;

    @GetMapping(value = "/page")
    @ResponseBody
    public Map findHospitalByPage(@RequestParam("currentpage") Integer currentpage, @RequestParam("limit")Integer limit){
        List<Hospital> page = hospitalService.findByPage(currentpage, limit);
        PageInfo<Hospital> sysUserPageInfo = new PageInfo<>(page);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(page);
        return jsonResult.getValues();
    }

    @GetMapping(value = "/get/{id}")
    @ResponseBody
    public Map findHospitalById(@PathVariable("id") Integer id){
        Hospital hospital = hospitalService.findById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(hospital);
        return jsonResult.getValues();
    }


    @PostMapping("/addhospital")
    @ResponseBody
    public Map register(@RequestBody Hospital hospital) {
        System.out.println(hospital);
        String name = hospital.getName();
        String linkman = hospital.getLinkman();

        Boolean aBoolean=null;
        //1、接收前端数据，并做数据校验(数据格式的合法性，使用第三方工具)（前后端都要做）
        if ((name!=null&&!name.equals(""))&&(linkman!=null&&!linkman.equals(""))){
            //2、如果数据合法，调用对应的service执行登录验证方法(通过spring给service对象注入)
            aBoolean = hospitalService.register(hospital);
        }
        //3、把验证结果给前端反馈
        JsonResult jsonResult = new JsonResult();
        if (aBoolean){
            jsonResult.setCode("200");
            jsonResult.setMsg("注册成功："+aBoolean);
        }else {
            jsonResult.setMsg("注册失败：请检查填写是否完整");
            //throw new BusinessException("登录失败，请检查用户工号或密码");
        }
        return jsonResult.getValues();
    }

    @PostMapping("/updatehospital")
    @ResponseBody
    public Map updatehospital(@RequestBody Hospital hospital) {
        System.out.println(hospital);
        String name = hospital.getName();
        String linkman = hospital.getLinkman();

        Boolean aBoolean1=null;
        //1、接收前端数据，并做数据校验(数据格式的合法性，使用第三方工具)（前后端都要做）
        if ((name!=null&&!name.equals(""))&&(linkman!=null&&!linkman.equals(""))){
            //2、如果数据合法，调用对应的service执行登录验证方法(通过spring给service对象注入)
            aBoolean1 = hospitalService.updateById(hospital);
        }
        //3、把登录验证结果给前端反馈
        JsonResult jsonResult = new JsonResult();
        if (aBoolean1){
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功："+aBoolean1);
        }else {
            jsonResult.setMsg("修改失败：请检查填写是否完整");
            //throw new BusinessException("登录失败，请检查用户工号或密码");
        }
        return jsonResult.getValues();
    }


}
